﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Jzy.Model.CRM;
using Frame.Lib.Core.Base;
using Frame.Lib.Utilities;

namespace Jzy.DAL.CRM
{
    public class CustomerDAL : BaseDAL
    {
        public void LogicDelete(List<int> ids)
        {
            string strIDs = ConvertUtil.ConvertToDbStr(ids);

            string sql = "update " + OTableName.Customer + " set IsDelete=1 where ID in (" + strIDs + ");" +
                        "update " + OTableName.CustomerContact + " set IsDelete=1 where CustomerID in (" + strIDs + ");" +
                        "update " + OTableName.CustomerVisit + " set IsDelete=1 where CustomerID in (" + strIDs + ");";
            dbOper.RunSql(sql);
        }

        public void ChangeUser(List<int> ids, int userID, string userName)
        {
            string strIDs = ConvertUtil.ConvertToDbStr(ids);

            string sql = "update " + OTableName.Customer + " set UserID=@p0, UserName=@p1 where ID in (" + strIDs + ")";

            dbOper.RunSql(sql, userID, userName);
        }

        public bool Exist(string name, object id)
        {
            string sql = "select count(*) from " + OTableName.Customer + " where IsDelete = 0 and Name = @p0";
            if (id != null)
            {
                if (id.ToString() != "")
                {
                    sql += " and ID != @p1";
                }
            }
            int count = dbOper.GetScalar<int>(sql, name, id);

            bool flag = (count > 0) ? true : false;
            return flag;
        }

        public bool ExistCode(string code, object id)
        {
            string sql = "select count(*) from " + OTableName.Customer + " where IsDelete = 0 and  Code = @p0";
            if (id != null)
            {
                if (id.ToString() != "")
                {
                    sql += " and ID != @p1";
                }
            }
            int count = dbOper.GetScalar<int>(sql, code, id);

            bool flag = (count > 0) ? true : false;
            return flag;
        }
    }
}
